package hk.ust.comp3111.ustnutrition;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

public class FeedbackDialog extends DialogFragment
{
	public interface FeedbackDialogListener
	{
		public void onSendFeedback(String feedback);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		ContextThemeWrapper theme = new ContextThemeWrapper(getActivity(),
				R.style.Theme_AppCompat_Light);
		LayoutInflater inflater = LayoutInflater.from(getActivity())
				.cloneInContext(theme);
		final View dialogView = inflater.inflate(R.layout.feedback_dialog, null);
		OnClickListener okListener = new OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						TextView text = (TextView)dialogView.findViewById(
								R.id.feedback);
						mListener.onSendFeedback(text.getText().toString());
					}
				};

		AlertDialog.Builder builder = new AlertDialog.Builder(theme);
		Dialog d = builder.setTitle(R.string.feedback_title)
				.setView(dialogView)
				.setPositiveButton(android.R.string.ok, okListener)
				.setNegativeButton(android.R.string.cancel, null)
				.create();
		return d;
	}

	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		if (activity instanceof FeedbackDialogListener)
		{
			mListener = (FeedbackDialogListener)activity;
		}
		else
		{
			throw new IllegalStateException("Parent activity must implement FeedbackDialogListener");
		}
	}

	private FeedbackDialogListener mListener;
}
